
package com.wctj.api.web.controller;

import com.wctj.api.base.enums.PayModeEnums;
import com.wctj.api.base.Result;
import com.wctj.api.base.annotation.CheckSign;
import com.wctj.api.base.annotation.SysLog;
import com.wctj.api.business.common.dto.*;
import com.wctj.api.web.handle.PayManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * 发起支付入口
 */
@Slf4j
@RestController
@RequestMapping("process")
public class PayController {
    @Resource
    private PayManagerService payManagerService;

    /**
     * 根据开发平台的支付订单号，获取支付订单体信息
     * 针对实时支付
     *
     * @param payOrderNo 参数
     * @return 跳到支付页面
     */
    @GetMapping(value = "infoByPayOrderNo")
    public Result<DdwxPaymentRecordDTO> infoByPayOrderNo(@RequestParam(name = "payOrderNo") String payOrderNo) {

        return Result.success(payManagerService.infoByPayOrderNo(payOrderNo));
    }

    /**
     * 第三方支付平台返回的支付对象 id
     * @param paymentIdDTO
     * @return
     */
    @PostMapping(value = "infoByThirdPartyPaymentId")
    public Result<Map> infoByThirdPartyPaymentId(@RequestBody @Valid PaymentIdDTO paymentIdDTO) {

        return Result.success(payManagerService.infoByThirdPartyPaymentId(paymentIdDTO));
    }



    /**
     * 跳到支付页面
     * 针对实时支付
     *
     * @param param 参数
     * @return 跳到支付页面
     */
    @PostMapping(value = "toPay")
    public Result<Object> toPay(@RequestBody @Valid PayOrderDTO param) {

        return payManagerService.toPay(param);
    }


    /**
     * 提现
     * @param withdrawalOrderDTO 参数
     * @return 跳到支付页面
     */
    @SysLog("提现")
    @PostMapping(value = "toWithdrawal")
    @CheckSign
    public Result<Object> toWithdrawal(@RequestBody @Valid WithdrawalOrderDTO withdrawalOrderDTO) {
        return payManagerService.toWithdrawal(withdrawalOrderDTO);
    }

    /**
     * 单笔支付转账
     * @param singleTransferDTO
     * @return
     */
    @SysLog("单笔支付转账")
    @PostMapping(value = "toSingleTransfer")
    @CheckSign
    public Result<Object> toSingleTransfer(@RequestBody @Valid SingleTransferDTO singleTransferDTO) {
        return payManagerService.toSingleTransfer(singleTransferDTO);
    }

    /**
     * 批量支付转账
     * @param batchTransferDTO
     * @return
     */
    @SysLog("批量支付转账")
    @PostMapping(value = "toBatchTransfer")
    @CheckSign
    public Result<Object> toBatchTransfer(@RequestBody @Valid BatchTransferDTO batchTransferDTO) {
        return payManagerService.toBatchTransfer(batchTransferDTO);
    }

    /**
     * 支付回调地址
     *  eg: callbackUrl/cmb
     * @param request 请求
     * @param payMode 支付模式 {@link PayModeEnums}
     * @return 是否成功
     */
    @SysLog("支付回调")
    @PostMapping(value = "callbackUrl/{payMode}")
    public Result<String> payBack(@PathVariable("payMode") String payMode, HttpServletRequest request) {

        return Result.success(payManagerService.payBack(request, payMode));
    }


    /**
     * 模拟第三方-支付回调地址
     *
     * @param jsonBody 请求
     * @return 是否成功
     */
    @PostMapping(value = "testCallbackUrl")
    public Result<String> testPayBack(@RequestBody String jsonBody) {
        log.info("testPayBack request param:{}",jsonBody);
        return Result.success(jsonBody);
    }


    /**
     * 查询支付订单记录列表
     * @param paymentRecordDTO
     * @return
     */
    @SysLog("查询支付订单记录列表")
    @PostMapping(value = "getPaymentRecordList")
    @CheckSign
    public Result<Object> getPaymentRecordList(@RequestBody @Valid PaymentRecordDTO paymentRecordDTO) {
        return payManagerService.getPaymentRecordList(paymentRecordDTO);
    }

}
